//
//  AlarmReportVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/27.
//
// 【首页-报警通知】报警通知-报警统计

import Foundation
import UIKit

class AlarmReportVC: SubLevelViewController {

    let reportV = AlarmReportView()
    
    var dateType: AlarmDateEnum = .daily
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .hex("#3C72FF")
        setupNavItems()
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        setupUI()

        reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navBarStyle(.custom, bg: "#3C72FF")
    }
    
    func reloadData() {
        view.showToastActivity()
        /// 预警统计
        API.getAlarmStatics(withParam: AlarmStaticsParam(date: date, dateType: "\(dateType.rawValue)", tbCompanyId: "\(deptId ?? -1)")) { responseModel in
            DispatchQueue.main.async {
                self.reportV.reloadUI(withModel: responseModel.model)
                self.view.hideToastActivity()
            }
        }
    }
    
    // MARK: - Actions
    
    @objc func shareTapped() {
        
    }
    
    @objc func deptTapped() {
        let bSearchVC = BranchSearchVC()
        bSearchVC.didSelectBranchWith = { (dept, _) -> () in
            self.deptName = dept.deptName
            self.deptId = dept.deptId
            self.reportV.deptBtn.setTitle(dept.deptName, for: .normal)
            self.reloadData()
            bSearchVC.navigationController?.popViewController(animated: true)
        }
        navigationController?.pushViewController(bSearchVC, animated: true)
    }
    
    // MARK: - Setup
    func setupUI() {
        reportV.baseV.dateV.didSelectItemWith = { (year, month, day, dateEnum) -> () in
            let m = month ?? 1
            let d = day ?? 1
            self.date = "\(year)-\(m >= 10 ? "\(m)" : "0\(m)")-\(d >= 10 ? "\(d)" : "0\(d)")"
            self.dateType = dateEnum
            self.reloadData()
        }
        reportV.baseV.handleWith = { (m) -> () in
            let vc = AlarmTypeStatisticsVC(withAlarmType: m.dictValue ?? "", name: m.dictLabel ?? "", deptId: self.deptId, deptName: self.deptName)
            self.navigationController?.pushViewController(vc, animated: true)
        }
        reportV.baseV.dateV.resetUI(type: .daily)
        reportV.deptBtn.setTitle(deptName, for: .normal)
        reportV.deptBtn.addTarget(self, action: #selector(deptTapped), for: .touchUpInside)
        view.addSubview(reportV)
        reportV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
    
    func setupNavItems() {
        backBtn.setImage(UIImage(systemName: "xmark"), for: .normal)
        backBtn.tintColor = .white
        
        let settingBtn = UIButton(type: .custom)
        settingBtn.setImage(UIImage(named: "ic_share"), for: .normal)
        settingBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        settingBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
        settingBtn.addTarget(self, action: #selector(shareTapped), for: .touchUpInside)
        let settingBar = UIBarButtonItem(customView: settingBtn)
        navigationItem.rightBarButtonItems = [settingBar]
        
    }
}
